package com.cj.wtrails.fragment


import android.annotation.SuppressLint

import android.os.Bundle
import android.os.Handler

import androidx.viewpager2.widget.ViewPager2
import com.cj.wtrails.BaseFragment
import com.cj.wtrails.R
import com.cj.wtrails.TvMainActivity

import com.cj.wtrails.adapter.InfoAdapter1
import com.cj.wtrails.bean.Rank
import com.cj.wtrails.utils.LogUtil
import com.cj.wtrails.utils.setCurrent
import kotlinx.android.synthetic.main.info_fragment.*


/**
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 *
 *
 * Created by yi on 2020/7/20.
 */
class InfoFragment : BaseFragment() {

    //private var infoAdapter: InfoAdapter? = null

    private val infoAdapter1 by lazy {
        InfoAdapter1()
    }

    private var list: List<Rank>? = null

    private val handler = Handler()

    private val time: Long = 2000

    private var currentInt = 0

    private var isPlan = true

    override fun getContentLayoutId(): Int {
        return R.layout.info_fragment
    }


    /*fun newInstance(list: ArrayList<Rank>?): InfoFragment {
        val args = Bundle()
        args.putParcelableArrayList("ranks", list)
        val fragment = InfoFragment()
        fragment.arguments = args
        return fragment
    }*/

    override fun show() {}
    override fun findView() {
        //infoAdapter = InfoAdapter(listOf("title1", "title2", "title3", "title4", "title5"))
        viewpager2.adapter = infoAdapter1
        //infoAdapter1.addData(listOf("title1", "title2", "title3", "title4", "title5"))
        //viewpager2.setPageTransformer(true, ScaleInTransformer())
        //play()
        //list = arguments?.getParcelableArrayList<Rank>("ranks")
        setData()


    }

    fun setData() {
        list = (activity as TvMainActivity).list
        if (list == null) {
            infoAdapter1.setEmptyView(R.layout.layout_empty, viewpager2)
        } else {
            infoAdapter1.setNewData(list)
            if (isPlan) {
                isPlan = false
                play()
            }
        }
    }


    @SuppressLint("ClickableViewAccessibility")
    override fun init() {
        viewpager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
                if (list != null) {
                    if (state == ViewPager2.SCROLL_STATE_IDLE) {
                        play()
                    }
                }
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                LogUtil.e(position.toString())
            }
        })

    }

    override fun setListener() {}
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            //pause
            LogUtil.e("InfoFragment不可见")
            stop()
        } else {
            //resume
            LogUtil.e("InfoFragment可见")
            if (list != null) {
                play()
            }
        }
    }

    private fun play() {
        handler.postDelayed({
            currentInt++
            viewpager2?.setCurrent(currentInt, 1000, viewpager2.width)
        }, time)
    }

    private fun stop() {
        handler.removeCallbacksAndMessages(null)
    }


}